using UnityEngine;

public class Minesweeper : MonoBehaviour
{
    // Entities and their states / Model
    private int rows = 10;
    private int cols = 10;
    private int mines = 10;
    private int[,] board;
    private bool[,] revealed;
    private bool[,] flagged;
    private bool gameOver;
    private Color darkGray = new Color(0.3f, 0.3f, 0.3f);
    private Color lightGray = new Color(0.9f, 0.9f, 0.9f);
    // 定义 "Restart" 按钮的位置和大小
    private Rect restartButtonRect = new Rect(200, 340, 100, 30);

    void Start()
    {
        InitializeGame();
    }

    // View render entities / models
    void OnGUI()
    {
        // 显示剩余地雷数
        int remainingMines = mines - CountFlaggedMines();
        GUI.Label(new Rect(0, 0, 200, 30), "Remaining Mines: " + remainingMines);
        if (GUI.Button(restartButtonRect, "Restart")) InitializeGame();

        // 检查剩余地雷数是否为0
        if (remainingMines == 0 && !gameOver)
        {
            gameOver = true;
        }

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                Rect rect = new Rect(j * 30, i * 30 + 30, 30, 30); // 调整矩形位置以避免覆盖剩余地雷数
                if (revealed[i, j])
                {
                    GUI.backgroundColor = darkGray; // 设置初始背景色为深灰色
                    if (board[i, j] == -1)
                    {
                        GUI.Button(rect, "X");
                    }
                    else if (board[i, j] == 0)
                    {
                        GUI.Button(rect, "");
                    }
                    else
                    {
                        GUI.Button(rect, board[i, j].ToString());
                    }
                }
                else if (flagged[i, j])
                {
                    GUI.backgroundColor = darkGray; // 设置初始背景色为深灰色
                    if (GUI.Button(rect, "F"))
                    {
                        flagged[i, j] = false;
                    }
                }
                else
                {
                    GUI.backgroundColor = lightGray; // 设置翻开后背景色为浅灰色
                    if (GUI.Button(rect, ""))
                    {
                        GUI.backgroundColor = lightGray; // 设置翻开后背景色为灰色
                        if (Event.current.button == 0) // 左键点击
                        {
                            Reveal(i, j);
                            
                        }
                        else if (Event.current.button == 1) // 右键点击
                        {
                            Flag(i, j);
                        }
                    }
                }
            }
        }
    
        if (gameOver)
        {
            GUI.Label(new Rect(0, rows * 30 + 30, 200, 50), "Game Over!");
            if (GUI.Button(restartButtonRect, "Restart"))
            {
                InitializeGame();
            }
        }
    }

    // Components /controls
    void InitializeGame()
    {
        board = new int[rows, cols];
        revealed = new bool[rows, cols];
        flagged = new bool[rows, cols];
        gameOver = false;

        // 生成地雷
        for (int i = 0; i < mines; i++)
        {
            int x, y;
            do
            {
                x = Random.Range(0, rows);
                y = Random.Range(0, cols);
            } while (board[x, y] == -1);
            board[x, y] = -1;
        }

        // 计算每个方块周围的地雷数量
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                if (board[i, j] == -1) continue;

                int count = 0;
                for (int dx = -1; dx <= 1; dx++)
                {
                    for (int dy = -1; dy <= 1; dy++)
                    {
                        int nx = i + dx;
                        int ny = j + dy;
                        if (nx >= 0 && nx < rows && ny >= 0 && ny < cols && board[nx, ny] == -1)
                        {
                            count++;
                        }
                    }
                }
                board[i, j] = count;
            }
        }
    }

    void Reveal(int x, int y)
    {
        if (revealed[x, y]) return;

        revealed[x, y] = true;

        if (board[x, y] == -1)
        {
            gameOver = true;
            return;
        }

        if (board[x, y] == 0)
        {
            for (int dx = -1; dx <= 1; dx++)
            {
                for (int dy = -1; dy <= 1; dy++)
                {
                    int nx = x + dx;
                    int ny = y + dy;
                    if (nx >= 0 && nx < rows && ny >= 0 && ny < cols)
                    {
                        Reveal(nx, ny);
                    }
                }
            }
        }
    }

    void Flag(int x, int y)
    {
        flagged[x, y] = !flagged[x, y];
    }

    int CountFlaggedMines()
    {
        int count = 0;
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                if (flagged[i, j])
                {
                    count++;
                }
            }
        }
        return count;
    }
}